/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui.gui.actions;

import java.awt.event.ActionEvent;
import java.io.File;
import javax.swing.AbstractAction;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import ui.gui.documents.AbstractDocument;
import ui.gui.documents.EditorFactory;
import ui.gui.documents.toolbox.Workspace;

/**
 *
 * @author stephane
 */

public class OpenFileAction extends AbstractAction
{
    private static final long serialVersionUID = 1L;
    
    public OpenFileAction(JFrame parent)
    {
        super("Open a file ...", new javax.swing.ImageIcon(OpenFileAction.class.getResource("/resources/openFile.png")));
        putValue(SHORT_DESCRIPTION, "Open a file in this workspace"); 
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        JFileChooser fileChooser = new JFileChooser();
        
        for (int i = 0; i < EditorFactory.getInstance().getExtensionFilter().length;i++) {	
	        fileChooser.addChoosableFileFilter(new DefaultFileFilter(EditorFactory.getInstance().getExtensionFilter()[i], EditorFactory.getInstance().getDescriptionFilter()[i]));
	
        }
        fileChooser.showOpenDialog(null);
        File file = fileChooser.getSelectedFile();
        if (file !=null && file.isFile())
        {
            AbstractDocument document = EditorFactory.getInstance().createFromFile(file);
            Workspace.getInstance().AddDocument(document);
        }
    }
    
}